package online.inote.model;

import javax.persistence.Table;

import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

import com.alibaba.fastjson.serializer.ValueFilter;

import lombok.Data;
import lombok.EqualsAndHashCode;
import online.inote.common.base.BaseEntity;
import online.inote.common.constant.ArticleType;

/**
 * <p>
 * Desc: 
 * </p>
 *
 * @author Sui
 * @created: 2017年12月3日 上午12:50:02
 * @version 1.0
 */
@Data
@EqualsAndHashCode(callSuper=false)
@Table(name = "t_article")
@Document(indexName = "inote", type = "article")
public class ArticleEntity extends BaseEntity {

	private static final long serialVersionUID = 1344574356868007467L;

	@Field(type = FieldType.Keyword)
	private String title;       // 标题
	@Field(type = FieldType.Keyword)
    private String summary;     // 概要
	@Field(type = FieldType.Keyword)
    private String keywords;    // 关键字
	@Field(type = FieldType.Integer)
    private Integer status;     // 状态
	@Field(type = FieldType.Boolean)
    private Boolean enable;     // 是否显示(0:不显示  1:显示)
	@Field(type = FieldType.Integer)
    private Integer type;    	// 类型(详见ArticleTypeEnum)
	@Field(type = FieldType.Keyword)
    private String authorId;    // 作者ID
	@Field(type = FieldType.Keyword)
    private String author;      // 作者姓名
	@Field(type = FieldType.Integer)
    private Integer source;     // 来源(1:原创  2:转载)
	@Field(type = FieldType.Keyword)
    private String cover;		// 封面
	@Field(type = FieldType.Integer)
    private Integer views;      // 查看次数
	@Field(type = FieldType.Keyword)
    private String staticUrl;	// 静态页面地址
    
    public static final ValueFilter jsonValueFilter() {
    	return new ValueFilter() {
            @Override
            public Object process(Object o, String propertyName, Object propertyValue) {
                if(propertyName.equals("type")){
                   return ArticleType.getName((int) propertyValue, true);    //返回修改后的属性值对象
                }
                
                if (propertyName.equals("cover")) {
                	return "/resources/common/images/404.png";
                }

                return propertyValue;
            }
        };
    }
}